package com.haiyang.flowablespringboot.controller;

import com.haiyang.flowablespringboot.entity.DeploymentDTO;
import com.haiyang.flowablespringboot.entity.ProcessDefinitionDTO;
import com.haiyang.flowablespringboot.entity.TaskVO;
import com.haiyang.flowablespringboot.service.MyService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class MyController {

    @Autowired
    private MyService myService;

    //启动流程实例
    @RequestMapping(value = "/process",method = RequestMethod.POST)
    public void startProcessInstance(){
        myService.startProcess();
    }

    //获取指定办理人任务
    @RequestMapping(value = "/tasks",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TaskVO> getTask(@RequestParam String assignee){
      return    myService.getTasks(assignee).stream().map(e-> new TaskVO(e.getId(),e.getName())).collect(Collectors.toList());
    }

    //部署流程
    @RequestMapping(value = "/deploy",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deploy(@RequestParam String path){
        myService.processDeploy(path);
    }


    //部署流程3
    @RequestMapping(value = "/deploy3",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deploy(){
        myService.processDeploy3();
    }

    //部署流程4
    @RequestMapping(value = "/deploy4",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deploy4() throws IOException {
        myService.processDeploy4();
    }

    //部署流程5
    @RequestMapping(value = "/deployZip",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deployZip() throws Exception {
        myService.deployZip();
    }

    //部署流程6
    @RequestMapping(value = "/deployBytes",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deployBytes() throws Exception {
        myService.deployBytes();
    }

    @RequestMapping(value = "/deployBpmnModel",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deployBpmnModel() {
        myService.deployBpmnModel();
    }

    //获取流程部署数据列表
    @RequestMapping(value = "/getDeployList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public List<DeploymentDTO> getDeployList() {
        return myService.getDeploymentList().stream().map(e-> new DeploymentDTO(e.getId(),e.getName())).collect(Collectors.toList());
    }

    //获取流程定义数据列表
    @RequestMapping(value = "/getProcessDefinitionList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public List<ProcessDefinitionDTO> getProcessDefinitionList() {
      return myService.getProcessDefinitionList().stream().map(e-> new ProcessDefinitionDTO(e.getId(),e.getName())).collect(Collectors.toList());
    }
    //获取部署流程图
    @RequestMapping(value = "/getProcessDiagram",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void getProcessDiagram(@RequestParam String processDefinitionId) {
         myService.getProcessDiagram(processDefinitionId);
    }
    //获取流程XML内容
    @RequestMapping(value = "/getProcessXMLText",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void getProcessXMLText(@RequestParam String processDefinitionId) {
        myService.getProcessXMLText(processDefinitionId);
    }

    @RequestMapping(value = "/deleteDeployment",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public void deleteDeployment(@RequestParam String deploymentId) {
        myService.deleteDeployment(deploymentId);
    }
}
